package com.zky.gulimall.product.service.impl;

import com.zky.gulimall.product.entity.AttrEntity;
import com.zky.gulimall.product.service.AttrService;
import com.zky.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.zky.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.Query;

import com.zky.gulimall.product.dao.AttrGroupDao;
import com.zky.gulimall.product.entity.AttrGroupEntity;
import com.zky.gulimall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cateLogId) {
        IPage<AttrGroupEntity> page;
        if(cateLogId==null || cateLogId==0){
            page=this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );
        }else {
            Object key = params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id",cateLogId);
            if(Objects.nonNull(key)){
                queryWrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            page=this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);

        }
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //查出当前分类下所有属性分组
        List<AttrGroupEntity> catelog_id = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if(CollectionUtils.isEmpty(catelog_id)){
            return null;
        }
        //查出每个分组所有属性
        List<AttrGroupWithAttrsVo> attrsVos = catelog_id.stream().map(e -> {
            AttrGroupWithAttrsVo withAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(e,withAttrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(e.getAttrGroupId());
            withAttrsVo.setAttrs(relationAttr);
            return withAttrsVo;
        }).collect(Collectors.toList());
        return attrsVos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //查出当前spu对应的所有属性分组信息以及当前分组下的所有属性对应的值
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
    }

}